﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.WebControls;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class InstrumentInfo : BasePage
    {
        public Guid InstrumentID
        {
            get
            {
                if (hiddenInstrumentID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenInstrumentID.Value);
            }
            set
            {
                hiddenInstrumentID.Value = value.ToString();
            }
        }
        public Guid CategoryID
        {
            get
            {
                if (hiddenCategoryID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenCategoryID.Value);
            }
            set
            {
                hiddenCategoryID.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                ddlcategoryGUID.BindDDDW(new CommonBll().dddw("insurmentcategory"), "categoryGUID", "categoryName");
                if (Request.QueryString["instrumentID"] != null)
                {
                    WsmInstrumentInfoEntity entity = new SystemBll().Get<WsmInstrumentInfoEntity>(new Guid(Request.QueryString["instrumentID"]));
                    if (entity != null) {
                        InstrumentID = entity.InstrumentID;
                        instrumentCode.Text = entity.instrumentCode;
                        instrumentName.Text = entity.instrumentName;
                        instrumentStandard.Text = entity.instrumentStandard;
                        unit.Text = entity.Unit;
                        vendor.Text = entity.Vendor;
                        PresellPrice.Text = entity.PresellPrice.ToString();                        
                        AjustRate.Text = entity.AjustRate.ToString();
                        ddlcategoryGUID.SelectedValue = entity.CategoryGUID.ToString();
                    }
                }

                if (Request.QueryString["categoryGUID"] != null && Request.QueryString["instrumentID"] == null)
                {
                    ddlcategoryGUID.SelectedValue = Request.QueryString["categoryGUID"].ToString();
                    CategoryID = new Guid(Request.QueryString["categoryGUID"].ToString());
                }
            }
        }
        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            WsmInstrumentInfoEntity entity = new WsmInstrumentInfoEntity();
            if (InstrumentID != Guid.Empty)
            {
                entity = new SystemBll().Get<WsmInstrumentInfoEntity>(InstrumentID);      
            }
            else {
                entity.CreatedOn = DateTime.Now;
                entity.Deleted = false;
                InstrumentID = Guid.NewGuid();
                entity.InstrumentID = InstrumentID;
                entity.CategoryGUID = CategoryID;
                InstrumentID = entity.InstrumentID;
            }

            entity.CategoryGUID = new Guid(ddlcategoryGUID.SelectedValue);
            entity.instrumentCode  = instrumentCode.Text.Trim();
            entity.instrumentName = instrumentName.Text.Trim();
            entity.instrumentStandard  = instrumentStandard.Text.Trim();
            entity.Unit = unit.Text.Trim();
            entity.Vendor = vendor.Text.Trim();
            if (PresellPrice.Value != null)
                entity.PresellPrice = System.Convert.ToDecimal(PresellPrice.Value);
            if(AjustRate.Value!= null)
            entity.AjustRate = System.Convert.ToDecimal(AjustRate.Value);
            new SystemBll().InsertOrUpdate(entity);
            //更新存货信息表（包括仪器、试剂等）
            CtrtInstrumentEntity entity1 = new CtrtInstrumentEntity();
            if (InstrumentID != Guid.Empty)
            {
                entity1 = new SystemBll().Get<CtrtInstrumentEntity>(InstrumentID);
                if (entity1 == null)
                { 
                    entity1 = new CtrtInstrumentEntity();
                    entity1.instrumentGUID = InstrumentID;
                    entity1.CreatedOn = DateTime.Now;
                    entity1.Deleted = false;
                }
            }
            else
            {
                entity1.CreatedOn = DateTime.Now;
                entity1.Deleted = false;
                entity1.instrumentGUID = InstrumentID;
            }

            entity1.categoryGUID = ddlcategoryGUID.SelectedValue.ToString();
            entity1.instrumentCode = instrumentCode.Text.Trim();
            entity1.instrumentName = instrumentName.Text.Trim();
            entity1.instrumentStandard = instrumentStandard.Text.Trim();
            entity1.unit = unit.Text.Trim();
            entity1.Type = 2;
            entity1.vendor = vendor.Text.Trim();
            if (PresellPrice.Value != null)
                entity1.PresellPrice = System.Convert.ToDecimal(PresellPrice.Value);
            
            new SystemBll().InsertOrUpdate(entity1);


            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');", true);
        }
    }
}
